package com.hzx.cloudcore.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by huzuxing on 2021/4/20.
 */
public class RedisUtils {
    private static int MAX_WAIT_TIME = 15 * 1000;

    private static int TIME_OUT = 10 * 1000;

    private static JedisPool jedisPool = null;

    public static class JedisStatus {
        public static final long FAIL_LONG = -5L;
        public static final int FAIL_INT = -5;
        public static final String FAIL_STRING = "-5";
    }

    private static void initPool() {

        String host = "127.0.0.1";
        int port = 6379;
        String auth = "";
        int maxTotal = 10;
        int maxIdle = 8;
        int minIdle = 8;
        boolean testOnBorrow = true;
        boolean testOnReturn = true;
        boolean testWhileIdle = true;
        int minEvictableIdleTimeMillis = 3000;
        int timeBetweenEvictionRunsMillis = 6000;
        int numTestsPerEvictionRun = 1000;
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(maxTotal);
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_TIME);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setTestOnBorrow(testOnBorrow);
        jedisPoolConfig.setTestOnReturn(testOnReturn);
        jedisPoolConfig.setTestWhileIdle(testWhileIdle);
        jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        try {
            if (null != auth && !"".equals(auth)) {
                jedisPool = new JedisPool(jedisPoolConfig, host, port, TIME_OUT, auth);
            }
            else {
                jedisPool = new JedisPool(jedisPoolConfig, host, port, TIME_OUT);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (null != jedisPool) {
                jedisPool.close();
            }
        }
    }

    static {
        initPool();
    }

    private static synchronized void poolInit() {
        if (null == jedisPool) {
            initPool();
        }
    }

    public static Jedis getJedis() {
        if (null == jedisPool) {
            poolInit();
        }

        Jedis jedis = null;
        try {
            if (null != jedisPool) {
                jedis = jedisPool.getResource();
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        }

        return jedis;
    }

    @SuppressWarnings("")
    public static void returnResource(Jedis jedis) {
        if (null != jedis && null != jedisPool) {
            jedisPool.returnResource(jedis);
        }
    }

    @SuppressWarnings("")
    public static void returnBrokenResource(Jedis jedis) {
        if (null != jedis && null != jedisPool) {
            jedisPool.returnBrokenResource(jedis);
        }
    }

    /**
     *
     * @param key
     * @param value
     * @return
     */

    public static String set(String  key, String value) {
        Jedis jedis = getJedis();
        if (null == jedis) {
            return JedisStatus.FAIL_STRING;
        }
        String res = null;

        try {
            res = jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return res;
    }

    public static String set(String key, String value, int expireTime) {
        Jedis jedis = getJedis();
        if (null == jedis) {
            return JedisStatus.FAIL_STRING;
        }
        String res = null;

        try {
            res = jedis.set(key, value);
            jedis.expire(key, expireTime);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return res;
    }

    public static String get(String key) {
        Jedis jedis = getJedis();
        if (null == jedis) {
            return JedisStatus.FAIL_STRING;
        }
        String res = null;
        try {
            res = jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return res;
    }

    public static long expire(String key, int seconds) {
        Jedis jedis = getJedis();
        if (null == jedis) {
            return JedisStatus.FAIL_LONG;
        }
        long res = 0;
        try {
            res = jedis.expire(key, seconds);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return res;
    }

    public static boolean exists(String key) {
        Jedis jedis = getJedis();
        if (null == jedis) {
            return false;
        }

        boolean res = false;
        try {
            res = jedis.exists(key);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return res;

    }
}
